package com.xonro.common.histrans.sfeg.job.update.contarct;

import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.schedule.IJob;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.consts.DrpConst;
import org.apache.commons.collections4.CollectionUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.math.BigDecimal;
import java.util.List;

public class updateContractTotalPay implements IJob {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SDK.getLogAPI().consoleInfo("开始进入同步已付款金额====" );
        List<RowMap> contractMap = DBSql.getMaps("SELECT ID,CONTRACT_NO  FROM BO_XR_MDM_CONTRACT where LP_TOTAL_PAYMENT_RECEIVED = 0 ");

        for (RowMap map : contractMap) {
            String contractNo = map.getString("CONTRACT_NO");
            String id = map.getString("ID");
            SDK.getLogAPI().consoleInfo("打印申请号=======" + contractNo);
            List<RowMap> accountMap = SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_CCID).getMaps("SELECT a.applyOutTotal applyOutTotal FROM tb_ContractMoneyOut a LEFT JOIN tb_ContractInfo b ON a.ctCode=b.ctCode  WHERE  b.HTBH='" + contractNo + "'AND a.isSubmit!='0' ");
            if (CollectionUtils.isEmpty(accountMap)) {
                continue;
            }
            double total = 0;
            for (RowMap account : accountMap) {
                double applyOutTotal = account.getLong("applyOutTotal");
                total += applyOutTotal;
            }

            SDK.getLogAPI().consoleInfo("获取修改状态====" + contractNo);
            SDK.getLogAPI().consoleInfo("获取修改的申请单号====" + contractNo);
            BigDecimal totalBig = new BigDecimal(total+"");
            totalBig= totalBig.setScale(2);
            DBSql.update("UPDATE BO_XR_MDM_CONTRACT SET LP_TOTAL_PAYMENT_RECEIVED='" + totalBig.toString() + "' WHERE ID='" + id + "' ");


        }
        SDK.getLogAPI().consoleInfo("结束进入同步已付款金额====");

    }
}
